import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import Utils from './../../../utils';
import { Control } from 'react-keeper';
import update from 'react-addons-update';
import moment from 'moment';
import DB from './../../../app/db';

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    //操作类型
    operType: 'add',
    //操作类型
    isNew : true,

    //数据主键ID
    spId: '',
    //是否锁定
    readOnly: false,

    //是否上传
    isUpload: false,
    //选中的工程
    currentEng: {},
    jdy:[],
    //保存成功弹出提示
    showToptips: false,

    showWarn: false,
    warnTimer: null,
    codeTimer: null,
    warnMessage: '',

  },

  initMode: function () {
    let t = this;
    t.data.operType == 'add';

    this.data.jlmode = update(this.data.jlmode,  {$set: {}});
    this.data.jlmode.jlr = update(this.data.jlmode.jlr,  {$set: '13972608122'});
    this.data.jlmode.router = update(this.data.jlmode.router,  {$set: '/myyewu/shenbaoybby'});
    this.data.jlmode.jllxName = update(this.data.jlmode.jllxName,  {$set: '隐蔽报验'});
    this.data.jlmode.jllx = update(this.data.jlmode.jllx,  {$set: '0035'});
    this.data.isNew = update(this.data.isNew,  {$set: true});
    this.data.isUpload = update(this.data.isUpload,  {$set: false});
    this.data.currentEng =  update(this.data.currentEng,  {$set: {}});
    this.data.jlmode.jdy =  update(this.data.jlmode.jdy,  {$set: []});
    this.data.jlmode.byXmmc =  update(this.data.jlmode.byXmmc,  {$set: '主体分部验收'});

    this.data.jdy =  update(this.data.jdy,  {$set: []});

    var jlrq = new Date();
    this.data.jlmode.jlrq = update(this.data.jlmode.jlrq,  {$set: jlrq.getTime()});
    this.data.jlmode.bysj = update(this.data.jlmode.bysj,  {$set: jlrq.getTime()});
    this.data.jlmode.yjyssj = update(this.data.jlmode.yjyssj,  {$set: jlrq.getTime()});

    t.updateComponent();
  },

  loadMode: function (params) {
    let t = this;
    this.data.jlmode = update(this.data.jlmode,  {$set: params});
    this.data.jlmode.id = update(this.data.jlmode.id,  {$set: params.id});
    this.data.currentEng =  update(this.data.currentEng,  {$set: params.eng});
    this.data.jdy =  update(this.data.jdy,  {$set: []});
    this.data.jdy = update(this.data.jdy,  {$set: this.data.jlmode.jdrys});

    this.data.jlmode.bz = update(this.data.jlmode.sgjd,  {$set: params.bz});
    this.data.jlmode.byXmmc = update(this.data.jlmode.ysxmmc,  {$set: params.byXmmc});
    this.data.jlmode.bysj = update(this.data.jlmode.bysj,  {$set: params.bysj});
    this.data.jlmode.bysj =  moment(this.data.jlmode.bysj).format('YYYY-MM-DD');
    this.data.jlmode.yjyssj = update(this.data.jlmode.yjyssj,  {$set: params.yjyssj});
    this.data.jlmode.yjyssj =  moment(this.data.jlmode.yjyssj).format('YYYY-MM-DD');
    this.data.isUpload = update(this.data.isUpload,  {$set: params.isUpload});

    this.data.isNew = update(this.data.isNew,  {$set: false});
    this.data.readOnly = false;
    this.data.operType = 'update';

    t.updateComponent();
  },


  showWarnMessage: function (message) {

    this.data.warnMessage = update(this.data.warnMessage, {$set: message});
    this.data.showWarn = update(this.data.showWarn, {$set: true});
    this.data.warnTimer = setTimeout(()=> {
      this.data.showWarn = update(this.data.showWarn, {$set: false});
      this.updateComponent();
    }, 3000);

    this.updateComponent();
  },

  onSetCurrentEng: function (obj) {
    let t = this;
    this.data.currentEng = update(this.data.currentEng, {$set: obj});

    this.data.jlmode = update(this.data.jlmode,{eng : {$set: obj}}  );

    obj.jdry.forEach(function(value,index,array){
      let ry = {name:'',emplId:'',userId:''};
      ry.name = value.ryName;
      ry.emplId = value.empId;
      ry.userId = value.userId;
      t.data.jdy[index] = ry;
    });

    this.updateComponent();
  },

  addRenYuan(obj){
    this.data.jdy.add(obj);
  },

  onSaveChange: function (obj) {
    this.data.jlmode.id = update(this.data.jlmode.id,  {$set: obj});
    this.data.isNew = update(this.data.isNew,  {$set: false});
    this.data.readOnly = false;
    this.data.operType = 'update';
    this.updateComponent();
  },

  onSaveInfo: function (obj) {
    let t = this;

    let resultEng = Utils.isEmptyObject(this.data.currentEng);
    console.info(resultEng.success);
    if (!resultEng.success)
    {
      this.showWarnMessage('工程不能为空');
      return;
    }

    this.data.jlmode.bysj =  moment(this.data.jlmode.bysj).format('YYYY-MM-DD hh:mm:ss');
    this.data.jlmode.yjyssj =  moment(this.data.jlmode.yjyssj).format('YYYY-MM-DD hh:mm:ss');
    DB.WechatAPI.saveYBBYInfo(this.data.jlmode).then(
      function (content) {
        t.onSaveChange(content.code);
      },
      function (error) {
        console.log(error);
      });
    this.updateComponent();
  },


  onUpInfo: function (obj) {
    let resultEng = Utils.isEmptyObject(this.data.currentEng);

    if (!resultEng.success)
    {
      this.showWarnMessage('工程不能为空');
      return;
    }
    let sp = {id: this.data.jlmode.id};
    console.log('id');
    console.info(sp);
    DB.WechatAPI.upYBBYInfo(sp).then(
      function (content) {
        Control.go(-1);
      },
      function (error) {
        console.log(error);
      });
    this.updateComponent();
  },

  onJlrqChange: function (params) {

    this.data.jlmode.jlrq = update(this.data.jlmode.jlrq,  {$set: params});
    this.updateComponent();
  },

  onSqRqChange: function (params) {
    this.data.jlmode.bysj = update(this.data.jlmode.bysj,  {$set: params});
    this.updateComponent();
  },

  onSqBlur: function (params) {
    this.data.jlmode.bysj = update(this.data.jlmode.bysj,  {$set: params});
    this.updateComponent();
  },

  onYjysRqChange: function (params) {

    this.data.jlmode.yjyssj = update(this.data.jlmode.yjyssj,  {$set: params});
    this.updateComponent();
  },

  onYjysBlur: function (params) {
    this.data.jlmode.yjyssj = update(this.data.jlmode.yjyssj,  {$set: params});
    this.updateComponent();
  },


  onSgjdChange: function (params) {
    this.data.jlmode.byXmmc = update(this.data.jlmode.byXmmc,  {$set: params});
    this.updateComponent();
  },

  onTqqkChange: function (params) {
    this.data.jlmode.tqqk = update(this.data.jlmode.tqqk,  {$set: params});
    this.updateComponent();
  },

  onCheckChange: function () {
    let zjy ='';
    if(null!= this.data.jdy && null!= this.data.jdy.length>0)
    {
      this.data.jdy.forEach(function(value,index,array){
        if (value.checked)
        {
          zjy += value.name+' ';
        }
      });
    }
    this.data.jlmode.zjy = update(this.data.jlmode.zjy,  {$set: zjy});
    this.data.jlmode.jdrys = update(this.data.jlmode.jdrys,  {$set: this.data.jdy});

    this.updateComponent();
  },

  onCheckInfo: function (obj) {
    if(null!= this.data.jdy && null!= this.data.jdy.length>0)
    {
      this.data.jdy.forEach(function(value,index,array){
        if (value.emplId == obj.target.value)
        {
          value.checked = obj.target.checked;
        }
      });
    }
    this.updateComponent();
  },



  onTqwdChange: function (params) {
    this.data.jlmode.qwqk = update(this.data.jlmode.qwqk,  {$set: params});
    this.updateComponent();
  },

  onQksgChange: function (params) {
    this.data.jlmode.sgjzqk = update(this.data.jlmode.sgjzqk,  {$set: params});
    console.info(this.data.jlmode);
    this.updateComponent();
  },

  onQkjlChange: function (params) {
    this.data.jlmode.jlgzqk = update(this.data.jlmode.jlgzqk,  {$set: params});
    console.info(this.data.jlmode);
    this.updateComponent();
  },

  onQkwtChange: function (params) {
    this.data.jlmode.czwt = update(this.data.jlmode.czwt,  {$set: params});
    console.info(this.data.jlmode);
    this.updateComponent();
  },

  onQklsChange: function (params) {
    this.data.jlmode.jlzl = update(this.data.jlmode.jlzl,  {$set: params});
    console.info(this.data.jlmode);
    this.updateComponent();
  },

  onQkqtChange: function (params) {
    this.data.jlmode.bz = update(this.data.jlmode.bz,  {$set: params});
    this.updateComponent();
  },


  //添加上传图片
  onAddImage: function (params) {
    let t = this;
    t.updateComponent();
  },


  onUpdateStore: function () {
    let t = this;
  },

  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }


});
